package cave.game;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

public class SharedPreferences {
	
	private Activity activity;
	private HashMap<String, String> stringData;
	private HashMap<String, Integer> integerData;
	private HashMap<String, Float> floatData;
	private String filename;
	
	public SharedPreferences(Activity activity, String filename) {
		this.activity = activity;
		this.filename = filename;
		stringData = new HashMap<String, String>();
		integerData = new HashMap<String, Integer>();
		floatData = new HashMap<String, Float>();
	}
	
	public void savePreferences(){
		try {
			FileOutputStream fos = activity.openFileOutput(filename, Context.MODE_PRIVATE);
			DataOutputStream writer = new DataOutputStream(fos);
			writer.writeInt(stringData.size());
			writer.writeInt(integerData.size());
			writer.writeInt(floatData.size());
			String key;
			Iterator<String> i = stringData.keySet().iterator();
			while(i.hasNext()) {
				key = i.next();
				writer.writeUTF(key);
				writer.writeUTF(stringData.get(key));
			}
			i = integerData.keySet().iterator();
			while(i.hasNext()) {
				key = i.next();
				writer.writeUTF(key);
				writer.writeInt(integerData.get(key));
			}
			i = floatData.keySet().iterator();
			while(i.hasNext()) {
				key = i.next();
				writer.writeUTF(key);
				writer.writeFloat(floatData.get(key));
			}
			writer.close();
		} catch (Exception e) {
			Log.w(MainActivity.TAG, "SharedPreferences.save: failed: "+e);
		}
		
	}
	
	public void loadPreferences(){
		try {
			stringData = new HashMap<String, String>();
			FileInputStream fis = activity.openFileInput(filename);
			DataInputStream reader = new DataInputStream(fis);
			int strings = reader.readInt();
			int integers = reader.readInt();
			int floats = reader.readInt();
			for (int i = 0; i < strings; i++) {
				stringData.put(reader.readUTF(), reader.readUTF());
			}
			for (int i = 0; i < integers; i++) {
				integerData.put(reader.readUTF(), reader.readInt());
			}
			for (int i = 0; i < floats; i++) {
				floatData.put(reader.readUTF(), reader.readFloat());
			}
			reader.close();
		} catch (Exception e) {
			Log.w(MainActivity.TAG, "SharedPreferences.load: failed: "+e);
		}
	}
	
	public void addString(String key, String value) {
		if (key != null) {
			stringData.put(key, value);
		} else {
			Log.w(MainActivity.TAG, "SharedPreferences.addString: key is null");
		}
	}
	
	public String getString(String key) {
		if (key != null) {
			return stringData.get(key);
		} else {
			return null;
		}
	}
	
	public String getString(String key, String defaultValue) {
		if (key != null && stringData.get(key) != null) {
			return stringData.get(key);
		} else {
			return defaultValue;
		}
	}
	
	public void addInteger(String key, int value) {
		if (key != null) {
			integerData.put(key, value);
		} else {
			Log.w(MainActivity.TAG, "SharedPreferences.addInteger: key is null");
		}
	}
	
	public int getInteger(String key) {
		if (key != null) {
			return integerData.get(key);
		} else {
			return 0;
		}
	}
	
	public int getInteger(String key, int defaultValue) {
		if (key != null && integerData.get(key) != null) {
			return integerData.get(key);
		} else {
			return defaultValue;
		}
	}
	
	public void addFloat(String key, float value) {
		if (key != null) {
			floatData.put(key, value);
		} else {
			Log.w(MainActivity.TAG, "SharedPreferences.addFloat: key is null");
		}
	}
	
	public float getFloat(String key) {
		if (key != null) {
			return floatData.get(key);
		} else {
			return 0.0f;
		}
	}
	
	public float getFloat(String key, float defaultValue) {
		if (key != null && floatData.get(key) != null) {
			return floatData.get(key);
		} else {
			return defaultValue;
		}
	}

}
